﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CoreLibs;
using HUGATE.Utility;
using HUGATE.ItemObject;

namespace HUGATE.Item.Controls.Category
{
    /// <summary>
    ///  Creator : Nguyễn Đức Hiếu
    /// Date    : 13/12/2010
    /// Desc    : Quản lý Danh mục sản phẩm
    /// </summary>
    public partial class CategoryEdit : UserControlBase
    {
        #region Variables

        private string strListPage = "Category/Category.aspx";
        private int intCategoryID = 0;
        #endregion

        #region Properties

        public string ListPage
        {
            get { return strListPage; }
            set { strListPage = value; }
        }

        public int CatagoryID
        {
            get { return intCategoryID; }
            set { intCategoryID = value; }
        }

        public long ParentID
        {
            get { return Convert.ToInt32(ViewState["CategoryEdit.ParentID"]); }
            set { ViewState["CategoryEdit.ParentID"] = value; }
        }
        public int Level
        {
            get { return Convert.ToInt32((ViewState["CategoryEdit.Level"])); }
            set { ViewState["CategoryEdit.Level"] = value; }
        }

        #endregion

        #region Permissions

        String strAddPermission = "ITEM_Category_Add";
        String strEditPermission = "ITEM_Category_Edit";
        String strDeletePermission = "ITEM_Category_Delete";

        #endregion

        #region Support Functions

        /// <summary>
        /// Reset Control's data
        /// </summary>
        private void Clear()
        {
            txtObjectID.Text = "";
            txtObjectName.Text = "";
            txtDesc.Text = "";
            txtparentID.Text = "";
            chkIsDefault.Checked = false;
            chkIsActived.Checked = false;
            //chkIsSystem.Checked = false;
            EnableControl(true);
        }

        /// <summary>
        /// Enable/Disable controls
        /// </summary>
        /// <param name="bolEnable"></param>
        private void EnableControl(bool bolEnable)
        {
            txtObjectName.Enabled = bolEnable;
            txtDesc.Enabled = bolEnable;
            chkIsDefault.Enabled = bolEnable;
            chkIsActived.Enabled = bolEnable;
            chkIsActived_CheckedChanged(null, null);

            udpEdit.Update();
        }

        /// <summary>
        /// Load Object's info to controls
        /// </summary>
        public void LoadInfo()
        {
            ITEMCategory objCategory = new ITEMCategory();
            objCategory.CategoryID = intCategoryID;

            if (objCategory.LoadInfo())
            {
                chkAutoGen.Checked = true;
                txtObjectID.Enabled = false;
                txtObjectID.Text = objCategory.RefCategoryID;
                txtObjectName.Text = objCategory.CategoryName.Trim();
                txtparentID.Text = Session["NameParentCategoryID"].ToString();
                txtDesc.Text = objCategory.Description.Trim();
                chkIsDefault.Checked = objCategory.IsDefault;
                chkIsActived.Checked = objCategory.IsActived;
                hdfCatID.Value = objCategory.CategoryID.ToString();
                hdfParentID.Value = objCategory.ParentCategoryID.ToString();
                //chkIsSystem.Checked = objCategory.IsSystem;

                //EnableControl(!chkIsSystem.Checked);

            }
        }
        public void ClearData()
        {
            txtObjectID.Text = string.Empty;
            txtObjectName.Text = string.Empty;
            txtparentID.Text = string.Empty;
            Session["NameParentCategoryID"] = string.Empty;
            txtDesc.Text = string.Empty;
            chkIsDefault.Checked = false;
            chkIsActived.Checked = true;
            hdfCatID.Value = string.Empty;
            chkAutoGen.Checked = true;
            chkAutoGen.Visible = true;
            txtObjectID.Enabled = false;
            hdfParentID.Value = string.Empty;
        }
        public void SetTextBoxParentID()
        {
            txtparentID.Text = Session["NameParentID"].ToString();
        }
        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancel.Text = HUGATE.Utility.ResxManager.GetString("Btn_Cancel");
                btnUpdate.Text = HUGATE.Utility.ResxManager.GetString("Btn_Save");
                txtDesc.MaxLength = 2000;
                txtObjectName.MaxLength = 200;
            }

            ctrlparent.GetString += new ParentCategory.GetNameParentID(ctrlparent_GetString);
        }

        void ctrlparent_GetString(object sender, EventArgs e)
        {
            ITEMCategory objCategory = new ITEMCategory();
            DataRow objRow = sender as DataRow;
            if (ctrlparent.temp != null)
            {
                txtparentID.Text = Convert.ToString(objRow["CategoryName"]);
                ParentID = Convert.ToInt64(objRow["CategoryID"].ToString());
                Level = Convert.ToInt32(objRow["Level"]) + 1;
                hdfParentID.Value = objRow["CategoryID"].ToString();
            }
            else
            {
                txtparentID.Text = "Root";
                ParentID = 0;
                Level = 0;
                hdfParentID.Value = "0";
            }
            udpEdit.Update();
        }
        void ctrlparent_SavedEvent(object sender, EventArgs e)
        {
            mpesel.Hide();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ITEMCategory objCategory = new ITEMCategory();

            objCategory.CategoryName = txtObjectName.Text.Trim();
            if (txtparentID.Text == "Root")
            {
                objCategory.ParentCategoryID = Convert.ToInt32(hdfParentID.Value);
                objCategory.Level = 0;
            }
            else
            {
                objCategory.ParentCategoryID = Convert.ToInt32(hdfParentID.Value);
                objCategory.Level = Level;
            }
            objCategory.Description = txtDesc.Text.Trim();
            objCategory.IsDefault = chkIsDefault.Checked;
            objCategory.IsActived = chkIsActived.Checked;
            objCategory.CreatedUser = User.UserName;
            if (!chkAutoGen.Checked)
            {
                if (txtObjectID.Text.Trim().Length <= 0)
                {
                    GlobalFunction.ShowMessageBox(Page, "Bạn cần phải nhập mã ngành hàng nếu không chọn tự động sinh!");
                    return;
                }

                objCategory.RefCategoryID = txtObjectID.Text.Trim();
            }
            else
                objCategory.RefCategoryID = "_?_AUTO_?_";
            if (hdfCatID.Value.Trim().Length > 0)
            {
                objCategory.CategoryID = Convert.ToInt64(hdfCatID.Value.Trim());
                objCategory.UpdatedDate = DateTime.Now;
                objCategory.UpdatedUser = User.UserName;
                objCategory.Update();
            }
            else
            {
                objCategory.Insert();
            }
            RaisePostBackEvent(e);
            Clear();
            Session["NameParentCategoryID"] = "";
            Session["CategoryID"] = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Clear();
            RaisePostBackEvent(e);

        }
        //protected void chkIsSystem_CheckedChanged(object sender, EventArgs e)
        //{
        //    //mpeUpdate.Show();
        //    EnableControl(!chkIsSystem.Checked);
        //}

        protected void chkIsActived_CheckedChanged(object sender, EventArgs e)
        {
            //mpeUpdate.Show();
            //chkIsDefault.Enabled = chkIsActived.Checked && chkIsActived.Enabled;
            //if (!chkIsActived.Checked)
            //    chkIsDefault.Checked = false;
        }

        protected void lnkClose_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region Event Handler

        public delegate void SaveDelegate(object sender, EventArgs e);
        public event SaveDelegate SavedEvent = null;

        protected void RaisePostBackEvent(EventArgs e)
        {
            if (SavedEvent != null)
                SavedEvent(this, e);
        }

        #endregion

        protected void btnChooseCategory_Click(object sender, EventArgs e)
        {
            mpesel.Show();
            lblTitle2.Text = HUGATE.Utility.ResxManager.GetString("PopupTitle_SelParentCategory");
        }
    }
}